package com.threeox.commonlibrary.adapter.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.threeox.commonlibrary.util.CommonUtils;
import com.threeox.commonlibrary.util.ViewUtils;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.LogUtils;
import com.threeox.utillibrary.util.res.IdHelper;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @ClassName: AutoIntoValExpandAdapter
 *
 * @Description: Todo(自动注入的)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016/12/22 10:40
 *
 * @version 1.0
 */
@SuppressLint("all")
public class AutoIntoValExpandAdapter<T> extends CommonExpandAdapter<T> {

	/*private Map<Integer, Object> mAutoGroupVal = new HashMap<Integer, Object>();
	private Map<Integer, Object> mAutoChildVal = new HashMap<Integer, Object>();*/
	private String TAG = this.getClass().getSimpleName();
	protected IdHelper mIdHelper = null;
	protected ViewUtils mViewUtils = null;

	public AutoIntoValExpandAdapter() {
		super();
	}

	public AutoIntoValExpandAdapter(Context context, List<T> datas, String childName, int groupLayoutId, int childLayoutId) {
		super(context, datas, childName, groupLayoutId, childLayoutId);
	}

	public AutoIntoValExpandAdapter(Context context, List<T> datas, String childName) {
		super(context, datas, childName);
	}

	@Override
	public void converGroupView(BaseViewHolder holder, int groupPosition, int groupType, boolean isExpanded, T t) {
		parse(holder, t);
	}

	@Override
	public void converChildView(BaseViewHolder holder, int groupPosition, int childPosition, int childType, boolean isLastChild, Object data) {
		parse(holder, data);
	}

	private void parse(BaseViewHolder holder, Object obj) {
		try {
			List<Object> tags = holder.getViewTags();
			if (EmptyUtils.isNotEmpty(tags)) {
				for (Object tag : tags) {
					try {
						if (tag != null) {
							View view = holder.getView(tag);
                            injectView(obj, tag.toString(), view);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			/*if (obj instanceof JSONObject) {
				parseJson(holder, (JSONObject) obj, autoVal);
			} else {
				parseObj(holder, obj, autoVal);
			}*/
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析对象
	 * 
	 * @param holder
	 * @param obj
	 */
	private void parseObj(BaseViewHolder holder, Object obj,
			Map<Integer, Object> autoVal) {
		Class<? extends Object> _Cls = obj.getClass();
		if (!autoVal.isEmpty()) {
			// L.e("已经缓存啦...");
			Set<Integer> set = autoVal.keySet();
			for (Integer id : set) {
				try {
					Method method = _Cls.getMethod("get" + CommonUtils.captureName(autoVal.get(id).toString()));
					holder.setValue(id, method.invoke(obj));
				} catch (Exception e) {
					LogUtils.e(TAG, "注入失败:" + e.getMessage());
				}
			}
		} else {
			Field[] fields = _Cls.getDeclaredFields();
			for (Field field : fields) {
				try {
					String key = field.getName();
					if (mIdHelper == null)
						mIdHelper.newInstance(mContext);
					int id = mIdHelper.getViewId(key);
					Method method = _Cls.getMethod("get" + CommonUtils.captureName(key));
					holder.setValue(id, method.invoke(obj));
					autoVal.put(id, key);
				} catch (Exception e) {
					LogUtils.e(TAG, "注入失败:" + e.getMessage());
				}
			}
		}
	}

	/**
	 * 解析JSONObject
	 * 
	 * @param holder
	 * @param obj
	 */
	private void parseJson(BaseViewHolder holder, JSONObject obj,
			Map<Integer, Object> autoVal) {
		if (!autoVal.isEmpty()) {
			// L.e("已经缓存啦...");
			Set<Integer> set = autoVal.keySet();
			for (Integer id : set) {
				try {
					holder.setValue(id, obj.get(autoVal.get(id)));
				} catch (Exception e) {
					LogUtils.e(TAG, "注入失败:" + e.getMessage());
				}
			}
		} else {
			Set<String> set = obj.keySet();
			for (String key : set) {
				// L.e(getClass(), key + "-->" + jsonData.get(key));
				try {

					if (mIdHelper == null)
						mIdHelper.newInstance(mContext);
					int id = mIdHelper.getViewId(key);
					holder.setValue(id, obj.get(key));
					autoVal.put(id, key);
				} catch (Exception e) {
					LogUtils.e(TAG, "注入失败:" + e.getMessage());
				}
			}
		}
	}

	@Override
	public List<T> getDatas() {
		return null;
	}

	/**
	 * View 注入数据
	 *
	 * @param obj
	 * @param tag
	 * @param view
	 */
	protected void injectView(Object obj, String tag, View view) {
		if (mViewUtils == null)
			mViewUtils = ViewUtils.newInstance();
		mViewUtils.injectView(obj, tag.toString(), view);
	}

}
